﻿using System;
using System.Collections.Generic;

namespace KidsProtector.BusinessManagement
{
    public class Rate
    {
        private DataAccess.Rate Access;

        public Rate()
        {
            Access = new DataAccess.Rate();
        }

        public bool CreateRate(DataAccess.T_Rate rate)
        {
            return Access.CreateRate(rate);
        }

        public bool remove(long idRate)
        {
            return Access.remove(idRate);
        }

        public int Getnotation(long idSite, long idUser)
        {
            return Access.Getnotation(idSite, idUser);
        }

        public DataAccess.T_Rate GetRate(long idRate)
        {
            return Access.GetRate(idRate);
        }

        public DataAccess.T_Rate GetRate(long idSite, long idUser)
        {
            return Access.GetRate(idSite, idUser);
        }

        public bool ModifyRate(long idRate, int note)
        {
            return Access.ModifyRate(idRate, note);
        }

        public int GetCount(long idSite)
        {
            return Access.GetCount(idSite);
        }

        public bool Noter(DataAccess.T_User user, string site, int note)
        {
            BusinessManagement.Site siteBM = new BusinessManagement.Site();
            BusinessManagement.Rate rateBM = new BusinessManagement.Rate();
            DataAccess.T_Site t_site = siteBM.GetSite(site);
            if (t_site == null)
            {
                t_site = new DataAccess.T_Site();
                t_site.Adress = site;
                t_site.Moyenne = note;
                siteBM.CreateSite(t_site);
                t_site = siteBM.GetSite(site);
                DataAccess.T_Rate rate = new DataAccess.T_Rate();
                rate.note = note;
                rate.IdUser = user.Id;
                rate.IdSite = t_site.Id;
                CreateRate(rate);
                siteBM.UpdateMoy(rate.IdSite);
                return true;
            }
            else
            {
                DataAccess.T_Rate rate = GetRate(t_site.Id, user.Id);
                if (rate == null)
                {
                    rate = new DataAccess.T_Rate();
                    rate.IdSite = t_site.Id;
                    rate.IdUser = user.Id;
                    rate.note = note;
                    CreateRate(rate);
                }
                else
                    ModifyRate(rate.Id, note);

                SendConfirmation(rate);
                return true;
            }
        }

        public void SendConfirmation(DataAccess.T_Rate rate)
        {
            Access.SendConfirmation(rate);
        }
    }
}